import { Injectable } from '@angular/core';
import * as Collections from 'typescript-collections';


@Injectable()
export class JgongCreatorService {
  public serialization: number[][] =  [];
  constructor() { }

  public doCreate(ridMap: Collections.Dictionary<number, number[]> , 
    posMap: Collections.Dictionary<number, number>){

      let jgList = [1,2,3,4,5,6,7,8,9];
      if(posMap) {
        posMap.values().forEach((n, index, array) => {
          jgList.splice(jgList.indexOf(n), 1);
        });
      }
      this.serialization = [];
      this.ridWhenCreating(jgList, ridMap, posMap);
      

  }

  public ridWhenCreating(jgList: Array<number> , 
    ridMap: Collections.Dictionary<number, number[]> , 
    posMap: Collections.Dictionary<number, number> , ret = [] ): void{

    let pos = ret.length;
    if(posMap.keys().indexOf(pos) >= 0){
      let subl = jgList.slice();
      let retl = ret.slice();
      retl.push(posMap.getValue(pos));
      this.ridWhenCreating(subl, ridMap, posMap, retl);

    }

    jgList.sort()
    if(pos == 8 && jgList.length == 1){
      ret.push(jgList.shift());
      this.serialization.push(ret);
      return;
    }
    else if(pos == 9){
      this.serialization.push(ret);
      return;
    }

    for(var n = 0; n < jgList.length; n ++ ){
      let subl = jgList.slice();
      let retl = ret.slice();
      let nn = jgList[n];
      let ridList = ridMap.getValue(pos);
      if(ridList && ridList.indexOf(nn) >= 0){
        continue;
      }
      retl.push(nn);
      subl.splice(n, 1);
      this.ridWhenCreating(subl, ridMap, posMap, retl);
    }

  } 

}